import requests
from celery import shared_task
import os


@shared_task
def check_github_info(username):
    result_list = []
    token = os.environ.get('GITHUB_TOKEN')
    headers = {"Authorization": f"token {token}"}
    count = 1

    while True:
        url = f"https://api.github.com/users/{username}/repos?per_page=100&page={str(count)}"
        r = requests.get(url, headers=headers)
        info = r.json()
        if isinstance(info, list):
            if len(info) == 0:
                break
        else:
            break
        for item in info:
            result_list.append(
                {"repo_name": item["name"], "stars": item["stargazers_count"]}
            )
        count = count + 1

    return {"repos": result_list}
